var main = {};
var myScroll;
var canLoad=true;
(function ($) {
	
    main.init = function () {
        document.addEventListener('touchmove', function (e) {
            e.preventDefault();
        }, false);
    };

    /**
     * @func move()
     * @desc 监听滚动条的位置，异步加载列表
     */
    function move() {
    	if(!canLoad){
    		return;
    	}
        if (this.y < main.max + 400) {
        	canLoad=false;
            $("#tip").hide();
            $("#loading").show();
            var index=$(".goodsList li:last").data("id");
            var html = "";
            $.ajax({
        		type:"post",
        		async:true, 
	        	url:path+"/index/more",
	        	data:{
	        		index:index,
	        		offset:"10"
	        	},
        		success:function(resultjson){
        			
        			if('000' == resultjson.code){
        				var html = "";
        				for (var i = 0; i < resultjson.result.length; i++) {
        					var goods=resultjson.result[i];
        					html+= '<li data-id="'+goods.id+'">';
        					html+= '<a href="'+path+'/commodity/info?goodsId='+goods.id+'">';
        					html+= '<div class="imgCon">';
        					html+= '<img class="imgBg" src="resources/images/5_2.png">';
        					html+= '<img class="goodsImg" src="'+goods.picurl+'">';
        					html+= '</div>';
        					html+= '<div class="goodsName">'+goods.name+'</div>';
        					html+= '<div class="price">';
        					html+= '<span>￥'+goods.price+'元</span>';
        					html+= '<span class="priceOld">￥'+goods.originalPrice+'</span>';
        					html+= '</div>';
        					html+= '</a>';
        					html+= '</li>';
        				}	
        				$(".goodsList").append(html);
        				if(resultjson.result.length>=10){
 	  		            	$("#loading").hide();
 			  	            $("#tip").show();
 			  	            canLoad=true;
 	  		            }else{
 	  		            	$("#loading").hide();
 	  		            	$("#tip").hide();
 	  		            }
			            myScroll.refresh();
			            main.max = myScroll.maxScrollY;
        			}
        		}
        	})
            this.refresh();
            main.max = this.maxScrollY;
            console.log(main.max);
            $("#loading").hide();
            $("#tip").show();
        }
    }

    /**
     * @func main.scroll()
     * @desc 配置iscroll插件
     */
    main.scroll = function () {
        myScroll = new IScroll("#wrapper", {probeType: 3, mouseWheel: true, click: true, preventDefault: false});

        main.max = myScroll.maxScrollY;
        console.log(main.max);

        myScroll.on("scroll", move);
    }
})(jQuery);

main.init();

window.onload = function () {
    main.scroll();
};
